package sctu.edu.java.year2016.team06.yuweimei.day20160605.chapter12_1;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;

/**
 * 编写程序，实现读取文件时出现一个表示读取进度的进度条。可使用javax.swing包提供的输入流类ProgressMonitorInputStream.
 * Created by asus on 2016/6/6.
 */


public class JinDu {
    public static void main(String[] args) {
        final JFrame f = new JFrame("ProgressMonitor Sample");
        f.getContentPane().setLayout(new FlowLayout());
        JButton b = new JButton("Click me");
        f.getContentPane().add(b);
        f.pack();
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Thread() {
                    public void run() {
                        try {
                            InputStream in = new FileInputStream("bigfile.dat");
                            ProgressMonitorInputStream pm = new ProgressMonitorInputStream(f, "Reading a big file", in);
                            int c;
                            while ((c = pm.read()) != -1) {
                            }
                            pm.close();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }.start();
            }
        });
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

}


